package edu.zqu.instructionalMatters.project.controller;


import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.CalendarVO;
import edu.zqu.instructionalMatters.project.entity.Calendar;
import edu.zqu.instructionalMatters.project.entity.Semester;
import edu.zqu.instructionalMatters.project.mapper.CalendarMapper;
import edu.zqu.instructionalMatters.project.mapper.SemesterMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020/7/3 9:59
 * @Description: 校历 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "校历")
public class CalendarController {
    @Autowired
    private CalendarMapper calendarMapper;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private SemesterMapper semesterMapper;

    @ApiOperation(value = "获取校历信息列表")
    @GetMapping(value = "/calendars")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = false),
            @ApiImplicitParam(name = "theDate", value = "日期", paramType = "query", required = false),
            @ApiImplicitParam(name = "holiday", value = "是否假期", paramType = "query", required = false)
    })
    public ZQUResult get(BigInteger collegeId,
                         BigInteger semesterId,
                         Date theDate,
                         Boolean holiday) {
        Calendar calendar1 = new Calendar().setCollegeId(collegeId).setSemesterId(semesterId).setTheDate(theDate).setHoliday(holiday);
        String collegeName = collegeService.getCollegeName(collegeId);                      //学校名称
        List<CalendarVO> list = new ArrayList<>();                                          //初始化 VO
        List<Calendar> list1 = calendarMapper.findAll(calendar1);                            //获取 Calender 列表
        for (Calendar calendar : list1) {                                                    //循环遍历赋值
            CalendarVO calendarVO = new CalendarVO();                                       //初始化
            BeanUtils.copyProperties(calendar, calendarVO);                                  //对象复制赋值
            calendarVO.setCollegeName(collegeName);                                         //学校名称
            calendarVO.setSemesterName(null);                                               //学期名称
            Semester semester = semesterMapper.selectById(calendar.getSemesterId());
            if (semester != null) {
                calendarVO.setSemesterName(semester.getName());                             //学期名称
            }
            list.add(calendarVO);                                                           //加入到list
        }
        return ZQUResult.success(list);                                                     //返回成功搜索到的集合
    }

    @ApiOperation(value = "获取校历信息", response = CalendarVO.class)
    @GetMapping(value = "/calendar/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "校历id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        Calendar calendar = calendarMapper.selectById(id);                                  //获取校历信息对象
        CalendarVO calendarVO = new CalendarVO();                                           //初始化VO
        BeanUtils.copyProperties(calendar, calendarVO);                                      //对象复制赋值
        String collegeName = collegeService.getCollegeName(calendar.getCollegeId());        //获取学校名称
        calendarVO.setCollegeName(collegeName);                                             //学校名称
        Semester semester = semesterMapper.selectById(calendar.getSemesterId());            //获取学期对象
        calendarVO.setSemesterName(null);                                                   //学期名称
        if (semester != null) {
            calendarVO.setSemesterName(semester.getName());                                 //学期名称
        }
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", calendarVO);              //返回搜索对象
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/calendar")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = true),
            @ApiImplicitParam(name = "theDate", value = "日期", paramType = "query", required = true),
            @ApiImplicitParam(name = "week", value = "周次", paramType = "query", required = true),
            @ApiImplicitParam(name = "day", value = "星期", paramType = "query", required = true),
            @ApiImplicitParam(name = "holiday", value = "是否假期", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger semesterId,
                         Date theDate,
                         Integer week,
                         Integer day,
                         Boolean holiday,
                         BigInteger byWho) {
        Calendar calendar = new Calendar();               //准备数据模型
        calendar.setCollegeId(collegeId);                 //设置学校id
        calendar.setSemesterId(semesterId);               //设置学期id
        calendar.setTheDate(theDate);                     //设置日期
        calendar.setWeek(week);                           //设置周次
        calendar.setDay(day);                             //设置星期
        calendar.setHoliday(holiday);                     //是否假期
        calendar.setByWho(byWho);                         //设置操作者
        calendarMapper.insert(calendar);                  //执行插入
        return ZQUResult.success();                       //操作成功返回
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/calendar/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = true),
            @ApiImplicitParam(name = "theDate", value = "日期", paramType = "query", required = true),
            @ApiImplicitParam(name = "week", value = "周次", paramType = "query", required = true),
            @ApiImplicitParam(name = "day", value = "星期", paramType = "query", required = true),
            @ApiImplicitParam(name = "holiday", value = "是否假期", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger semesterId,
                            Date theDate,
                            Integer week,
                            Integer day,
                            Boolean holiday,
                            BigInteger byWho) {
        Calendar calendar = calendarMapper.selectById(id);//查出旧记录
        calendar.setByWho(byWho);                         //旧记录设置最后操作者
        calendarMapper.updateById(calendar);              //更新旧记录
        calendarMapper.deleteById(id);                    //逻辑删除旧记录
        calendar = new Calendar();                        //准备新纪录
        calendar.setCollegeId(collegeId);                 //设置学校id
        calendar.setSemesterId(semesterId);               //设置学期id
        calendar.setTheDate(theDate);                     //设置日期
        calendar.setWeek(week);                           //设置周次
        calendar.setDay(day);                             //设置星期
        calendar.setHoliday(holiday);                     //设置是否假期
        calendar.setByWho(byWho);                         //设置操作者
        calendar.setLastVersion(id);                      //设置上一版本
        calendarMapper.insert(calendar);                  //执行插入
        return ZQUResult.success();                       //操作成功返回
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/calendar/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        Calendar calendar = calendarMapper.selectById(id);//先查出旧记录
        calendar.setByWho(byWho);                         //修改操作者
        calendarMapper.updateById(calendar);              //更新记录
        calendarMapper.deleteById(id);                    //逻辑删除记录
        return ZQUResult.success();                       //操作成功返回
    }
}

